﻿var statesTask, riversTask, citiesTask, forthTask, query;
var statesInfoTemplate, riversInfoTemplate, citiesInfoTemplate, forthInfoTemplate;
var pointSym, lineSym, polygonSym;
require(["dojo/on", "esri/map", "esri/geometry/Point","esri/layers/ArcGISDynamicMapServiceLayer", "esri/toolbars/draw",
    "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", "esri/Color",
    "esri/tasks/QueryTask", "esri/tasks/query", "esri/InfoTemplate", "tdlib/TDTLayer", "tdlib/TDAnnoLayer", "dojo/domReady!"],
    function (on, Map,Point, ArcGISDynamicMapServiceLayer, Draw, SimpleMarkerSymbol,
        SimpleLineSymbol, SimpleFillSymbol, Color,
        QueryTask, Query, InfoTemplate) {
        var tb = new Draw(map);
        tb.on("draw-end", doQuery);
        on(document.getElementById("dk"), "click", activateTool);
        on(document.getElementById("reset"), "click", deactivateTool);
        var queryUrl = getMapUrl();
        statesTask = new esri.tasks.QueryTask(queryUrl + "/0");
        riversTask = new esri.tasks.QueryTask(queryUrl + "/1");
        citiesTask = new esri.tasks.QueryTask(queryUrl + "/2");
        forthTask = new esri.tasks.QueryTask(queryUrl + "/3");

        query = new esri.tasks.Query();
        query.returnGeometry = true;
        statesInfoTemplate = new esri.InfoTemplate("地块${GUID}", "<iframe src='http://114.141.186.229:82/TipPage.aspx?id=${GUID}' width='1366'  height='768'> </iframe>");
        riversInfoTemplate = new esri.InfoTemplate("${FID}", "ID：${FID}<br/><br/>开发区名称1：${RNAME}");
        citiesInfoTemplate = new esri.InfoTemplate("${OBJECTID}", "ID：${OBJECTID}<br/><br/>开发区名称2：${AREA}");
        forthInfoTemplate = new esri.InfoTemplate("开发区名称3");
        var redColor = new Color([255, 0, 0]);
        var halfFillYellow = new Color([255, 255, 0, 0.5]);
        pointSym = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 10,
                    new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, redColor, 1),
                    halfFillYellow);
        lineSym = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT, redColor, 2);
        polygonSym = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
                    new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, redColor, 2),
                    halfFillYellow);

        function activateTool() {     
               var tool = "POINT";
                tb.activate(Draw[tool]);
                active = true;
         
        }
        function deactivateTool() {     
                map.graphics.clear();
                map.infoWindow.hide();
                map.setZoom(8);
                map.centerAt(new Point(121.48, 31.22));             
                tb.deactivate();
                if (colored) {
                    map.removeLayer(featureLayer);
                }
                colored = false;
        }
        function doQuery(evt) {
            query.geometry = evt.geometry;
            var taskName = document.getElementById("task").value;
            var queryTask;
            if (taskName === "forthTask") {
                queryTask = forthTask;
                query.outFields = ["OBJECTID_1 ", "KFQMC"];
            }
            else if (taskName === "riversTask") {
                queryTask = riversTask;
                query.outFields = ["FID", "RNAME"];
            }
            else if (taskName === "citiesTask") {
                queryTask = citiesTask;
                query.outFields = ["OBJECTID", "YQMC"];
            }
            else if (taskName === "statesTask") {
                queryTask = statesTask;
                query.outFields = ["FID", "GUID"];
            }
            queryTask.execute(query, showResults);
        }

        function showResults(featureSet) {
            map.graphics.clear();
            var symbol, infoTemplate;
            var taskName = document.getElementById("task").value;
            switch (taskName) {
                case "citiesTask":
                    symbol = polygonSym;
                    infoTemplate = citiesInfoTemplate;
                    break;
                case "riversTask":
                    symbol = polygonSym;
                    infoTemplate = riversInfoTemplate;
                    break;
                case "statesTask":
                    symbol = polygonSym;
                    infoTemplate = statesInfoTemplate;
                    break;
                case "forthTask":
                    symbol = polygonSym;
                    infoTemplate = forthInfoTemplate;
                    break;
            }
            var resultFeatures = featureSet.features;
            for (var i = 0, il = resultFeatures.length; i < il; i++) {
                var graphic = resultFeatures[i];
                graphic.setSymbol(symbol);
                graphic.setInfoTemplate(infoTemplate);
                map.graphics.add(graphic);          
            }
        }
    });
